/*
引入依赖包
最低SDK版本要求：viapi20230117的SDK版本需大于等于2.0.1。
可以在此仓库地址中引用最新版本SDK：https://mvnrepository.com/artifact/com.aliyun/viapi20230117
<!-- https://mvnrepository.com/artifact/com.aliyun/viapi20230117 -->
<dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>viapi20230117</artifactId>
      <version>${aliyun.viapi.version}</version>
</dependency>
*/

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.tea.TeaModel;
import com.aliyun.viapi20230117.models.GetAsyncJobResultResponse;
import com.aliyun.viapi20230117.models.QueryAsyncJobListResponse;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


public class GetAsyncJobResult {
    public static com.aliyun.viapi20230117.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "viapi.cn-shanghai.aliyuncs.com";
        return new com.aliyun.viapi20230117.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        // "YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET" 的生成请参考https://help.aliyun.com/document_detail/175144.html
        // 如果您是用的子账号AccessKey，还需要为子账号授予权限AliyunVIAPIFullAccess，请参考https://help.aliyun.com/document_detail/145025.html
        com.aliyun.viapi20230117.Client client = GetAsyncJobResult.createClient(
                "LTAIBqFbkm0FZ1K6",
                "1inhunDMjbB7nsDvac2vI1JtPaRjBn");

        final String jobId = "CBD474D2-6EEF-5AB6-95BA-5E71AA28CB97";
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();

        com.aliyun.viapi20230117.models.QueryAsyncJobListRequest queryAsyncJobListRequest = new com.aliyun.viapi20230117.models.QueryAsyncJobListRequest()
                .setJobId(jobId)
                .setStartTime("2023-04-28 00:00:00")
                .setEndTime("2023-04-28 23:00:00");

        // 查询异步任务的完成情况
        try {
            QueryAsyncJobListResponse queryAsyncJobListResponse = client.queryAsyncJobListWithOptions(queryAsyncJobListRequest, runtime);
            /**
             * {"headers":
             * {"access-control-allow-origin":"*",
             * "date":"Fri, 28 Apr 2023 06:31:50 GMT",
             * "content-length":"429",
             * "x-acs-request-id":"D610DC5C-8862-5977-8A2E-35DFDE444406",
             * "connection":"keep-alive",
             * "content-type":"application/json;charset=utf-8",
             * "x-acs-trace-id":"b1a2cabd51b039de6c95d274b84e1ce3"},
             *
             * "body":
             * {"HttpCode":"200",
             * "RequestId":"D610DC5C-8862-5977-8A2E-35DFDE444406",
             * "Data":{"PageSize":20,"TotalPage":1,"CurrentPage":1,"TotalRecord":1,
             *      "Result":[{"Status":"PROCESS_FAILED", // Job执行状态
             * 		    "GmtCreate":"2023-04-28 14:26:00","EndTime":"2023-04-28 14:26:00",
             * 		    "CallerParentId":"1541287857323986",
             * 		    "PopApiName":"GenerateImageWithTextAndImage",
             * 		    "PopProduct":"imageenhan",
             * 		    "StartTime":"2023-04-28 14:26:00",
             * 		    "JobId":"15C0614A-B906-5C67-AABA-3D02D0BB4BF9" // JobId
             *                }] // Result里面是任务对象的列表
             *         }
             * },
             * "statusCode":200}
             */
            // 获取整体结果。
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(queryAsyncJobListResponse)));
            // 获取单个字段。
            String jobStatus = queryAsyncJobListResponse.getBody().getData().getResult().stream().filter(
                    job -> job.jobId.equals(jobId)
            ).map(str -> str.getStatus()).toArray(String[]::new)[0];
            System.out.println("jobStatus: " + jobStatus);

            if (jobStatus.equals("PROCESS_FAILED")) return;

        } catch (com.aliyun.tea.TeaException teaException) {
            // 获取整体报错信息。
            System.out.println("Async JobList ERROR: " + com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段。
            System.out.println("Async JobList ERROR: " + teaException.getCode());
        }

        com.aliyun.viapi20230117.models.GetAsyncJobResultRequest getAsyncJobResultRequest = new com.aliyun.viapi20230117.models.GetAsyncJobResultRequest()
                .setJobId(jobId);

        try {
            GetAsyncJobResultResponse getAsyncJobResultResponse = client.getAsyncJobResultWithOptions(getAsyncJobResultRequest, runtime);
            // 获取整体结果。
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(getAsyncJobResultResponse)));
            // 获取单个字段。
            System.out.println(getAsyncJobResultResponse.getBody());
        } catch (com.aliyun.tea.TeaException teaException) {
            // 获取整体报错信息。
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段。
            System.out.println(teaException.getCode());
        }
    }
}